package com.agd.quick.states;
//main
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import android.view.KeyEvent;
import lib.elementgame.GameAnim;
import lib.elementgame.GameSprite;
import lib.elementgame.Mfx;
import lib.elementgame.Sfx;
import lib.engine.Anchor;
import lib.engine.GameEngine;
import lib.engine.GameState;

public class StateMainMenu extends GameState {

	private static GameSprite backgroundFloor;
	private static GameSprite background;
	private static GameSprite background2;
	private static GameSprite background3;

//	private static GameSprite btnPlay;
//	private static GameSprite btnLeaderboards;
//	private static GameSprite btnGallery;
//	private static GameSprite btnOption;
//	private static GameSprite btnHelp;
//	private static GameSprite btnCredit;

	private static GameAnim btnPlay;
	private static GameAnim btnLeaderboards;
	private static GameAnim btnGallery;
	private static GameAnim btnOption;
	private static GameAnim btnHelp;
	private static GameAnim btnCredit;
	private static GameAnim btnMore;
	private static GameAnim btnMore1;
	
	private Rectangle bg_game;
	private GameAnim player;
	private GameAnim tidur;
	private GameAnim bangun;
	private boolean keluar;
	private boolean play = false;
	private boolean Setting_Vibration= true;
	private boolean tekan;
	private boolean efek;
	public StateMainMenu(GameEngine engine) {
		super(engine);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			ITouchArea pTouchArea, float pTouchAreaLocalX,
			float pTouchAreaLocalY) {
		// TODO Auto-generated method stub
		if (pSceneTouchEvent.isActionUp()) {
			if (pTouchArea == btnPlay && btnPlay.isVisible()) {
				btnPlay.setCurrentTileIndex(0);
				if(Setting_Vibration){
					engine.vibrate(200);
				}
				else{
					engine.vibrate(0);
				}
				Sfx.Play(SOUND_PILIH_MENU);
				Mfx.Play(SOUND_TRACTOR);
				play = true;
				if(play == true){
					tidur.setVisible(false);
					bangun.setVisible(true);
					bangun.animate(120, false);
				btnCredit.setVisible(false);
				btnGallery.setVisible(false);
				btnHelp.setVisible(false);
				btnLeaderboards.setVisible(false);
				btnOption.setVisible(false);				
				btnPlay.setVisible(false);
				btnMore.setVisible(false);
				btnMore1.setVisible(false);
				}
			}
			
			else if (pTouchArea == btnOption && btnOption.isVisible()) {
				btnOption.setCurrentTileIndex(0);
				if(Setting_Vibration){
					engine.vibrate(200);
				}
				else{
					engine.vibrate(0);
				}
				Sfx.Play(SOUND_PILIH_MENU);
				exitState(OptionState);
			}
			else if (pTouchArea == btnHelp && btnHelp.isVisible()) {
				btnHelp.setCurrentTileIndex(0);
				if(Setting_Vibration){
					engine.vibrate(200);
				}
				else{
					engine.vibrate(0);
				}
				Sfx.Play(SOUND_PILIH_MENU);
				exitState(HelpState);
			}
			else if (pTouchArea == btnCredit && btnCredit.isVisible()) {
				btnCredit.setCurrentTileIndex(0);
				if(Setting_Vibration){
					engine.vibrate(200);
				}
				else{
					engine.vibrate(0);
				}
				Sfx.Play(SOUND_PILIH_MENU);
				exitState(CreditState);
			}
			else if(pTouchArea == btnLeaderboards && btnLeaderboards.isVisible()){
				btnLeaderboards.setCurrentTileIndex(0);
				if(Setting_Vibration){
					engine.vibrate(200);
				}
				else{
					engine.vibrate(0);
				}
				Sfx.Play(SOUND_PILIH_MENU);
				exitState(LeaderBoardState);
			}
			else if(pTouchArea == btnGallery && btnGallery.isVisible()){
				btnGallery.setCurrentTileIndex(0);
				if(Setting_Vibration){
					engine.vibrate(200);
				}
				else{
					engine.vibrate(0);
				}
				Sfx.Play(SOUND_PILIH_MENU);
				exitState(AchievementsState);
			}
			else if(pTouchArea == btnMore && btnLeaderboards.isVisible()){
				btnMore.setCurrentTileIndex(0);
				btnMore1.setCurrentTileIndex(0);
				if(!btnHelp.isVisible()){
					btnHelp.setVisible(!false);
					btnOption.setVisible(!false);
					btnCredit.setVisible(!false);
					btnMore.setVisible(false);
					btnMore1.setVisible(true);
					tekan = true;
				}
				else{
					btnMore.setVisible(true);
					btnMore1.setVisible(false);
					tekan = false;
				}
				efek = true;
			}
			else{
				btnPlay.setCurrentTileIndex(0);
				btnOption.setCurrentTileIndex(0);
				btnHelp.setCurrentTileIndex(0);
				btnCredit.setCurrentTileIndex(0);
				btnLeaderboards.setCurrentTileIndex(0);
				btnGallery.setCurrentTileIndex(0);
				btnMore.setCurrentTileIndex(0);
				btnMore1.setCurrentTileIndex(0);
			}
		}
		else if (pSceneTouchEvent.isActionDown()) {
			if (pTouchArea == btnPlay) {
					btnPlay.setCurrentTileIndex(1);
			}
			
			if (pTouchArea == btnOption && btnOption.isVisible()) {
					btnOption.setCurrentTileIndex(1);
			}
			if (pTouchArea == btnHelp && btnHelp.isVisible()) {
				
					btnHelp.setCurrentTileIndex(1);
				
			}
			if (pTouchArea == btnCredit && btnCredit.isVisible()) {
					btnCredit.setCurrentTileIndex(1);
				
				
			}
			if(pTouchArea == btnLeaderboards && btnLeaderboards.isVisible()){
					btnLeaderboards.setCurrentTileIndex(1);
			}
			if(pTouchArea == btnGallery && btnGallery.isVisible()){
					btnGallery.setCurrentTileIndex(1);
			}
			if(pTouchArea == btnMore && btnLeaderboards.isVisible()){
				btnMore.setCurrentTileIndex(1);
				btnMore1.setCurrentTileIndex(1);
			}
		}
		else if (pSceneTouchEvent.isActionMove()){
			if (pTouchArea == btnPlay && btnPlay.isVisible()) {
					btnPlay.setCurrentTileIndex(1);
					btnOption.setCurrentTileIndex(0);
					btnHelp.setCurrentTileIndex(0);
					btnCredit.setCurrentTileIndex(0);
					btnLeaderboards.setCurrentTileIndex(0);
					btnGallery.setCurrentTileIndex(0);
					btnMore.setCurrentTileIndex(0);
					btnMore1.setCurrentTileIndex(0);
			}
			
			else if (pTouchArea == btnOption && btnOption.isVisible()) {
					btnOption.setCurrentTileIndex(1);
					btnPlay.setCurrentTileIndex(0);
					btnHelp.setCurrentTileIndex(0);
					btnCredit.setCurrentTileIndex(0);
					btnLeaderboards.setCurrentTileIndex(0);
					btnGallery.setCurrentTileIndex(0);
					btnMore.setCurrentTileIndex(0);
					btnMore1.setCurrentTileIndex(0);
			}
			else if (pTouchArea == btnHelp && btnHelp.isVisible()) {
				
					btnHelp.setCurrentTileIndex(1);
					btnPlay.setCurrentTileIndex(0);
					btnOption.setCurrentTileIndex(0);
					btnCredit.setCurrentTileIndex(0);
					btnLeaderboards.setCurrentTileIndex(0);
					btnGallery.setCurrentTileIndex(0);
					btnMore.setCurrentTileIndex(0);
					btnMore1.setCurrentTileIndex(0);
			}
			else if (pTouchArea == btnCredit && btnCredit.isVisible()) {
					btnCredit.setCurrentTileIndex(1);
					btnPlay.setCurrentTileIndex(0);
					btnOption.setCurrentTileIndex(0);
					btnHelp.setCurrentTileIndex(0);
					btnLeaderboards.setCurrentTileIndex(0);
					btnGallery.setCurrentTileIndex(0);
					btnMore.setCurrentTileIndex(0);
					btnMore1.setCurrentTileIndex(0);
			}
			else if(pTouchArea == btnLeaderboards && btnLeaderboards.isVisible()){
					btnLeaderboards.setCurrentTileIndex(1);
					btnPlay.setCurrentTileIndex(0);
					btnOption.setCurrentTileIndex(0);
					btnHelp.setCurrentTileIndex(0);
					btnCredit.setCurrentTileIndex(0);
					btnGallery.setCurrentTileIndex(0);
					btnMore.setCurrentTileIndex(0);
					btnMore1.setCurrentTileIndex(0);
			}
			else if(pTouchArea == btnGallery && btnGallery.isVisible()){
					btnGallery.setCurrentTileIndex(1);
					btnPlay.setCurrentTileIndex(0);
					btnOption.setCurrentTileIndex(0);
					btnHelp.setCurrentTileIndex(0);
					btnCredit.setCurrentTileIndex(0);
					btnLeaderboards.setCurrentTileIndex(0);
					btnMore.setCurrentTileIndex(0);
					btnMore1.setCurrentTileIndex(0);
			}
			else if(pTouchArea == btnMore && btnLeaderboards.isVisible()){
					btnMore.setCurrentTileIndex(1);
					btnMore1.setCurrentTileIndex(1);
					btnPlay.setCurrentTileIndex(0);
					btnOption.setCurrentTileIndex(0);
					btnHelp.setCurrentTileIndex(0);
					btnCredit.setCurrentTileIndex(0);
					btnLeaderboards.setCurrentTileIndex(0);
					btnGallery.setCurrentTileIndex(0);
			}
		}
		return false;
	}

	@Override
	public void initComponent() {
		// TODO Auto-generated method stub

		background = new GameSprite(GP_SPRITE_BACKGGROUND, engine);
		background2 = new GameSprite(GP_SPRITE_BACKGGROUND2, engine);
		background3 = new GameSprite(GP_SPRITE_BACKGGROUND3, engine);
		backgroundFloor = new GameSprite(MM_SPRITE_BACKGGROUND, engine);

		bg_game = new Rectangle(0, 0, 800, 480, engine.getVertexBufferObjectManager());
		
//		btnPlay = new GameSprite(MM_SPRITE_BTNPLAY, engine);
//		btnLeaderboards = new GameSprite(MM_SPRITE_BTNLEADERBOARDS, engine);
//		btnGallery = new GameSprite(MM_SPRITE_BTNGALLERY, engine);
//		btnOption = new GameSprite(MM_SPRITE_BTNOPTION, engine);
//		btnHelp = new GameSprite(MM_SPRITE_BTNHELP, engine);
//		btnCredit = new GameSprite(MM_SPRITE_BTNCREDIT, engine);
		
		btnPlay = new GameAnim(MM_ANIM_BTNPLAY, engine);
		btnLeaderboards = new GameAnim(MM_ANIM_BTNLEADERBOARD , engine);
		btnGallery = new GameAnim(MM_ANIM_BTNARCHIVEMENT , engine);
		btnOption = new GameAnim(MM_ANIM_BTNSETTING , engine);
		btnHelp = new GameAnim(MM_ANIM_BTNHELP , engine);
		btnCredit = new GameAnim(MM_ANIM_BTNCREDIT , engine);
		btnMore = new GameAnim(MM_ANIM_MORE, engine);
		btnMore1 = new GameAnim(MM_ANIM_MORE1, engine);
		
		player= new GameAnim(GP_ANIM_TIWI_LARI, engine);
		tidur = new GameAnim(MM_ANIM_TIDUR, engine);
		bangun = new GameAnim(GP_ANIM_TIWI_AWAL, engine);
	}

	@Override
	protected void init() {
		// TODO Auto-generated method stub
		if(Integer.parseInt(engine.getDatabase().getData(SETTING_VIBRATION, 0, 1)) == 0){
			Setting_Vibration = false;
		}
		else{
			Setting_Vibration = true;
		}
		keluar = false;
		tidur.animate(600);
		player.animate(200);
		tidur.setVisible(true);
		bangun.setVisible(false);
		player.setVisible(false);
		engine.camera.setCenter(400, 240);
		tekan=false;
		efek=false;
		bg_game.setColor(Color.WHITE);
        bg_game.setAlpha(0.1f);
		btnCredit.setVisible(false);
		btnHelp.setVisible(false);
		btnOption.setVisible(false);
		btnMore.setVisible(true);
		btnMore1.setVisible(false);
		btnMore.setCurrentTileIndex(0);
		btnMore1.setCurrentTileIndex(0);
			if(Integer.parseInt(engine.getDatabase().getData(SETTING_SOUND, 0, 1)) == 0){
				Mfx.Pause(BG_SOUND);
				Sfx.setVolume(SOUND_PILIH_MENU, 0);
				Sfx.setVolume(SOUND_GET_BENSIN, 0);
				Sfx.setVolume(SOUND_GET_PAGAR_PATUNG, 0);
				Sfx.setVolume(SOUND_GET_TELUR_ITEM, 0);
				Sfx.setVolume(SOUND_GET_TELUR_OBS, 0);
				Sfx.setVolume(SOUND_HIGHSCORE, 0);
				Sfx.setVolume(SOUND_NO_HIGHSCORE, 0);
				Sfx.setVolume(SOUND_NITROS, 0);
				Mfx.setVolume(SOUND_TRACTOR, 0);
			}
			else{
				Mfx.Play(BG_SOUND);
				Sfx.setVolume(SOUND_PILIH_MENU, 1);
				Sfx.setVolume(SOUND_GET_BENSIN, 1);
				Sfx.setVolume(SOUND_GET_PAGAR_PATUNG, 1);
				Sfx.setVolume(SOUND_GET_TELUR_ITEM, 1);
				Sfx.setVolume(SOUND_GET_TELUR_OBS, 1);
				Sfx.setVolume(SOUND_HIGHSCORE, 1);
				Sfx.setVolume(SOUND_NO_HIGHSCORE, 1);
				Sfx.setVolume(SOUND_NITROS, 1);
				Mfx.setVolume(SOUND_TRACTOR, 1);
			}

		background.setWidth(800);
		background.setHeight(550);
		background2.setWidth(800);
		background2.setHeight(450);
		background3.setWidth(800);
		background3.setHeight(450);

	}

	@Override
	protected void attach() {
		// TODO Auto-generated method stub
		engine.scene.attachChild(background3);
		engine.scene.attachChild(background2);
		engine.scene.attachChild(background);
		engine.scene.attachChild(bg_game);
		engine.scene.attachChild(backgroundFloor);
		engine.scene.attachChild(btnPlay);
		engine.scene.attachChild(btnLeaderboards);
		engine.scene.attachChild(btnGallery);
		engine.scene.attachChild(btnHelp);
		engine.scene.attachChild(btnOption);
		engine.scene.attachChild(btnCredit);
		engine.scene.attachChild(player);
		engine.scene.attachChild(tidur);
		engine.scene.attachChild(bangun);
		engine.scene.attachChild(btnMore);
		engine.scene.attachChild(btnMore1);
	}

	@Override
	protected void detach() {
		// TODO Auto-generated method stub
		background3.detachSelf();
		background2.detachSelf();
		background.detachSelf();
		backgroundFloor.detachSelf();
		bg_game.detachSelf();
		btnPlay.detachSelf();
		btnLeaderboards.detachSelf();
		btnGallery.detachSelf();
		btnOption.detachSelf();
		btnHelp.detachSelf();
		btnCredit.detachSelf();
		player.detachSelf();
		tidur.detachSelf();
		bangun.detachSelf();
		btnMore.detachSelf();
		btnMore1.detachSelf();
	}

	@Override
	protected void setPosition() {
		// TODO Auto-generated method stub
		background.setPosition(Anchor.BOTTOM_LEFT);
		background2.setX(0);
		background2.setY(-20);
		background3.setX(0);
		background3.setY(-40);
		backgroundFloor.setX(-20);
		player.setPosition(-50, 140, Anchor.CENTER);
		tidur.setPosition(0, 140, Anchor.CENTER);
		bangun.setPosition(-25, 140, Anchor.CENTER);
		btnPlay.setPosition(Anchor.CENTER);
		btnLeaderboards.setPosition(135, -10, Anchor.BOTTOM_LEFT);
		btnGallery.setPosition(-135, -10, Anchor.BOTTOM_RIGHT);
		btnOption.setPosition(10, -10, Anchor.BOTTOM_LEFT);;
		btnHelp.setPosition(10, -10, Anchor.BOTTOM_LEFT);;
		btnCredit.setPosition(10, -10, Anchor.BOTTOM_LEFT);;
		btnMore.setPosition(10, -10, Anchor.BOTTOM_LEFT);
		btnMore1.setPosition(10, -10, Anchor.BOTTOM_LEFT);
	}

	@Override
	protected void registerTouch() {
		// TODO Auto-generated method stub
		engine.scene.registerTouchArea(btnPlay);
		engine.scene.registerTouchArea(btnLeaderboards);
		engine.scene.registerTouchArea(btnGallery);
		engine.scene.registerTouchArea(btnOption);
		engine.scene.registerTouchArea(btnHelp);
		engine.scene.registerTouchArea(btnCredit);
		engine.scene.registerTouchArea(background);
		engine.scene.registerTouchArea(background2);
		engine.scene.registerTouchArea(background3);
		engine.scene.registerTouchArea(backgroundFloor);
		engine.scene.registerTouchArea(btnMore);
	}

	@Override
	protected void unregisterTouch() {
		// TODO Auto-generated method stub
		engine.unregisterSceneTouch(btnPlay);
		engine.unregisterSceneTouch(btnLeaderboards);
		engine.unregisterSceneTouch(btnGallery);
		engine.unregisterSceneTouch(btnOption);
		engine.unregisterSceneTouch(btnHelp);
		engine.unregisterSceneTouch(btnCredit);
		engine.unregisterSceneTouch(background);
		engine.unregisterSceneTouch(background2);
		engine.unregisterSceneTouch(background3);
		engine.unregisterSceneTouch(backgroundFloor);
		engine.unregisterSceneTouch(btnMore);
	}

	@Override
	protected void onUpdate() {
		// TODO Auto-generated method stub
		if(play == true){
			if(!bangun.isAnimationRunning()){
				bangun.setVisible(false);
				player.setVisible(true);
				player.setX(player.getX() + 4);
			}
			else{
				bangun.setX(bangun.getX() + 2);
				player.setX(player.getX() + 2);
			}
		}
		if(efek){
			if(tekan){
				if(btnCredit.collidesWith(btnMore)){
					btnCredit.setY(btnCredit.getY() - 16);
				}
				if(btnOption.collidesWith(btnCredit)){
					btnOption.setY(btnOption.getY() - 18);
				}
				if(btnHelp.collidesWith(btnOption)){
					btnHelp.setY(btnHelp.getY() - 20);
				}
				if(!btnCredit.collidesWith(btnMore) && !btnOption.collidesWith(btnCredit) && !btnHelp.collidesWith(btnOption)){
					efek = false;
				}
			}
			else{
				if(btnCredit.getY() < 300){
					btnCredit.setY(btnCredit.getY() + 8);
				}
				else{
					btnCredit.setPosition(10, -10, Anchor.BOTTOM_LEFT);
					btnCredit.setVisible(false);
				}
				if(btnOption.getY() < 300){
					btnOption.setY(btnOption.getY() + 10);
				}
				else{
					btnOption.setPosition(10, -10, Anchor.BOTTOM_LEFT);
					btnOption.setVisible(false);
				}
				if(btnHelp.getY()<300){
					btnHelp.setY(btnHelp.getY() + 12);
				}
				else{
					btnHelp.setPosition(10, -10, Anchor.BOTTOM_LEFT);
					btnHelp.setVisible(false);
				}
				if(!btnCredit.isVisible() && !btnOption.isVisible() && !btnHelp.isVisible()){
					efek = false;
				}
			}
		}
		if(player.getX() >= 805){
			play = false;
			btnCredit.setVisible(!false);
			btnGallery.setVisible(!false);
			btnHelp.setVisible(!false);
			btnLeaderboards.setVisible(!false);
			btnOption.setVisible(!false);
			btnPlay.setVisible(!false);
			exitState(TipsState);
		}
		if(keluar){
			exitState(GameQuitState);
		}
	}

	@Override
	protected void onPaused() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onResumed() {
		// TODO Auto-generated method stub

	}

	@Override
	public void onKeyUp(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK && btnGallery.isVisible()) {
			keluar = true;
		}
	}

	@Override
	public void initLoading() {
		// TODO Auto-generated method stub
		
	}

}
